<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
          size="mini"
          type="primary"
          style="margin-bottom: 20px"
          @click="handleReturn"
          >返回</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          plain
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:product1:export']"
          >下载</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-upload2"
          v-hasPermi="['system:product1:import']"
          size="mini"
          @click="handleImport"
          >导入</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      border
      v-loading="loading"
      :data="productList"
      :span-method="arraySpanMethod"
    >
      <el-table-column
        label="产品型号"
        align="center"
        prop="classification"
        width="200"
      />
      <el-table-column
        label="产品类型"
        align="center"
        prop="productType"
        width="200"
      />

      <!-- <el-table-column
        label="推荐产品"
        align="center"
        prop="recommendedProducts"
      /> -->
      <el-table-column label="施胶方法" align="center" prop="sizingMethod" />
      <!-- <el-table-column label="作用" align="center" prop="effect" />
      <el-table-column label="使用温度" align="center" prop="temperature" />
      <el-table-column label="材质" align="center" prop="texture" />
      <el-table-column label="表面状况" align="center" prop="surface" />
      <el-table-column label="粘度" align="center" prop="viscosity" />
      <el-table-column label="固化后强度" align="center" prop="strength" />
      <el-table-column label="固化时间" align="center" prop="curingTime" /> -->
      <el-table-column label="基材表面处理方法" prop="content">
        <template slot-scope="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="120"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox
              v-model="upload.updateSupport"
            />是否更新已经存在的用户数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改胶粘产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品型号" prop="classification">
          <el-input v-model="form.classification" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="产品类型" prop="productType">
          <el-input v-model="form.productType" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="推荐产品" prop="recommendedProducts">
          <el-input
            v-model="form.recommendedProducts"
            placeholder="请输入作用"
          />
        </el-form-item>

        <el-form-item label="施胶方法" prop="sizingMethod">
          <el-input v-model="form.sizingMethod" placeholder="请输入作用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
} from "@/api/glue/product";
import { getToken } from "@/utils/auth";

export default {
  name: "selectionList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 胶粘产品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productType: null,
        productDescription: null,
        recommendedProducts: null,
        effect: null,
        temperature: null,
        texture: null,
        surface: null,
        viscosity: null,
        strength: null,
        curingTime: null,
      },
      // 表单参数
      form: {
        classification: "",
        productType: "",
        recommendedProducts: "",
        sizingMethod: "",
      },
      // 表单校验
      rules: {
        classification: [
          { required: true, message: "产品型号不能为空", trigger: "change" },
        ],
        productType: [
          { required: true, message: "产品类型不能为空", trigger: "change" },
        ],
        recommendedProducts: [
          { required: true, message: "推荐产品不能为空", trigger: "change" },
        ],
        sizingMethod: [
          { required: true, message: "施胶方法不能为空", trigger: "change" },
        ],
      },

      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/product/import",
      },
    };
  },
  created() {
    if (this.$route.query) {
      this.queryParams = {
        ...this.$route.query,
      };
      this.queryParams.pageNum = this.queryParams.pageNum * 1;
      this.queryParams.pageSize = this.queryParams.pageSize * 1;
    }

    this.getList();
  },
  methods: {
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 3) {
        // 假设最后一列的索引是2（基于0的索引）
        if (rowIndex % this.queryParams.pageSize === 0) {
          return {
            rowspan: this.queryParams.pageSize,
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "胶粘剂信息导入";
      this.upload.open = true;
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },

    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "/system/product/exportTemplate",
        {},
        `胶粘产品导入模板_${new Date().getTime()}.xlsx`
      );
    },

    /** 查询胶粘产品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.productList.forEach((element) => {
          element.content = ` <div>
      <h3>金属（表面无镀层/面漆）</h3>
      <div>
        （1）清洁：无尘布用无水乙醇打湿后，采取单方向擦拭教材，避免来回擦，彻底清洁后至少晾干10分钟；
      </div>
      <div>
        （2）打磨：采用合适目数的砂纸斜向45度交叉打磨至表面出现明显的粗糙度；
      </div>
      <div>
        （3）再次清洁：方法同上，至最后一次擦拭无明显金属屑脏污，至少晾干10分钟。
      </div>
      <div>
        镀锌表面或有面漆时，则不能进行打磨，仅用无水乙醇进行彻底清洁即可。
      </div>
      <h3>热固性塑料（如SMC板材）</h3>
      <div>
        （1）清洁：无尘布用无水乙醇打湿后，采取单方向擦拭基材，避免来回擦，彻底清洁后至少晾干10分钟；
      </div>
      <div>
        （2）打磨：采用合适目数的砂纸或纤维布斜向45度交叉打磨至表面出现一定的粗糙度，注意千万不能过分打磨，避免玻纤外露；
      </div>
      <div>
        （3）再次清洁：方法同上，至最后一次擦拭无明显粉末脏污，至少晾干10分钟。
      </div>
      <div>
        热塑性塑料（如尼龙、PP） 仅做清洁，不推荐打磨（容易出现应力裂纹）
      </div>
      <div>
        清洁：方法同热固性塑料，但注意无水乙醇用量不宜过多、擦拭力度不宜过大。
      </div>
      <h3>橡胶</h3>
      <div>
        （1）清洁：无尘布用无水乙醇打湿后，采取单方向擦拭基材，避免来回擦，彻底清洁后至少晾干10分钟；擦拭力度不宜太大，容易使碳粉掉出。
      </div>
      <div>（2）打磨：轻微打磨至有一点粗糙度即可，用力不可过大；</div>
      <div>
        （3）再次清洁：方法同上，至最后一次擦拭无明显粉末脏污，至少晾干10分钟。
      </div>
    </div>`;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    handleReturn() {
      this.$router.go(-1);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classification: "",
        productType: "",
        recommendedProducts: "",
        sizingMethod: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加胶粘产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.form = row;
      this.open = true;
      this.title = "修改胶粘产品";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除胶粘产品编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProduct(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/product/export",
        {
          ...this.queryParams,
        },
        `胶粘产品列表_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style></style>
